package com.bjsc.storage;

import com.bjsc.storage.ftp.FtpClientFactory;
import com.bjsc.storage.ftp.FtpClientPool;
import com.bjsc.storage.ftp.FtpStorage;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author create by VIC.Huang
 * @date 2018/3/30
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "ftp")
public class FtpConfig {
    private String host;
    private int port;
    private String user;
    private String password;

    @Bean
    public FtpStorage storage() {
        FtpStorage ftpStorage = new FtpStorage();
        ftpStorage.setFtpClientPool(ftpClientPool());
        return ftpStorage;
    }

    private FtpClientPool ftpClientPool() {
        FtpClientFactory factory = new FtpClientFactory();
        factory.setHost(host);
        factory.setPort(port);
        factory.setUser(user);
        factory.setPassword(password);

        FtpClientPool pool = new FtpClientPool(factory);
        pool.setTestOnBorrow(true);
        pool.setTestOnReturn(true);
        pool.setTimeBetweenEvictionRunsMillis(5000);
        pool.setTestWhileIdle(true);
        pool.setMaxWaitMillis(10000);
        pool.setMinIdle(4);
        pool.setMaxIdle(10);

        return pool;
    }
}
